package com.tengju.bff.interfaces.manage.bonus.facade;

import com.tengju.bff.interfaces.shared.*;
import com.tengju.data.domain.model.clickhouse.subject.DwsSubjectSettleRepository;
import com.tengju.data.domain.model.clickhouse.subject.SubjectSkuStatistics;
import com.tengju.data.domain.model.clickhouse.subject.SubjectStatistics;
import com.tengju.data.domain.shared.BasePage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenhang
 * @Date 2021/4/21
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class SubjectFacade {


    private final DwsSubjectSettleRepository dwsSubjectSettleRepository;

    public TableVO statistics(Integer month) {

        List<SubjectStatistics> subjectStatisticsList = dwsSubjectSettleRepository.listSubjectStatistics(month);
        if (CollectionUtils.isEmpty(subjectStatisticsList)) {
            return null;
        }
        TableVO table = new TableVO();
        List<ColumnVO> header = List.of(
                ColumnVO.of("monthFormat","月份",false),
                ColumnVO.of("ownerName","主体",false),
                ColumnVO.of("moneyOut","出货额",false),
                ColumnVO.of("bonus","服务费",false),
                ColumnVO.of("statusDesc","状态",false)
        );
        table.setHeader(header);
        List<Map<String, Object>> records = new ArrayList<>();
        for (SubjectStatistics subjectStatistics : subjectStatisticsList) {
            Map<String, Object> row = new HashMap<>();
            row.put("monthFormat",subjectStatistics.getMonth().format("yyyy年MM月"));
            row.put("ownerName",subjectStatistics.getSubjectName());
            row.put("moneyOut",new AmountVO(AmountType.NONE,subjectStatistics.getMoneyOut()));
            row.put("bonus",new AmountVO(AmountType.NONE,subjectStatistics.getBonus()));
            row.put("statusDesc",subjectStatistics.getStatus().getDesc());
            records.add(row);
        }
        table.setRecords(records);
        return table;
    }

    public TableVO skuDetails(Integer month, Integer pageNo, Integer pageSize) {
        BasePage<SubjectSkuStatistics> pageSubjectStatistics = dwsSubjectSettleRepository.listSubjectSkuStatistics(month,pageNo,pageSize);
        List<SubjectSkuStatistics> subjectSkuStatisticsList = pageSubjectStatistics.getDatas();
        if (CollectionUtils.isEmpty(subjectSkuStatisticsList)) {
            return null;
        }
        TableVO table = new TableVO();
        List<ColumnVO> header = List.of(
                ColumnVO.of("monthFormat","月份",false),
                ColumnVO.of("ownerName","主体",false),
                ColumnVO.of("skuId","商品SKU ID",false),
                ColumnVO.of("skuName","商品标题",false),
                ColumnVO.of("brandId","品牌ID",false),
                ColumnVO.of("moneyOut","出货额",false),
                ColumnVO.of("bonus","服务费",false),
                ColumnVO.of("statusDesc","状态",false)
        );
        table.setHeader(header);
        List<Map<String, Object>> records = new ArrayList<>();
        for (SubjectSkuStatistics skuStatistics : subjectSkuStatisticsList) {
            Map<String, Object> row = new HashMap<>();
            row.put("monthFormat",skuStatistics.getMonth().format("yyyy年MM月"));
            row.put("ownerName",skuStatistics.getSubjectName());
            row.put("skuId",skuStatistics.getSkuId().getValue());
            row.put("skuName",skuStatistics.getSkuName());
            row.put("brandId",skuStatistics.getBrandId().getValue());
            row.put("moneyOut",new AmountVO(AmountType.NONE,skuStatistics.getMoneyOut()));
            row.put("bonus",new AmountVO(AmountType.NONE,skuStatistics.getBonus()));
            row.put("statusDesc",skuStatistics.getStatus().getDesc());
            records.add(row);
        }
        table.setRecords(records);
        PageResultVO page = new PageResultVO();
        page.setTotal(pageSubjectStatistics.getTotal());
        page.setPageIndex(pageNo);
        page.setPageSize(pageSize);
        table.setPage(page);
        return table;
    }

    public AmountVO summary(Integer month) {
        BigDecimal amount = dwsSubjectSettleRepository.summary(month);
        return new AmountVO(AmountType.NONE,amount);
    }
}
